<?php

namespace App\Kuafu\Order\Validate;

use App\Kuafu\Order\Enum\OrderMerchantEnum;
use App\Kuafu\Base\Validate\BaseValidate;
use Illuminate\Http\Request;

class OrderMerchantValidate extends BaseValidate
{
    public static function index(Request $request)
    {
        $rules    = [
            'showTimeTab'    => self::merchantCommonRules('showTimeTab'),
            'showStartAt'    => self::merchantCommonRules('showStartAt'),
            'showEndAt'      => self::merchantCommonRules('showEndAt'),
            'compareTimeTab' => self::merchantCommonRules('compareTimeTab'),
            'compareStartAt' => self::merchantCommonRules('compareStartAt'),
            'compareEndAt'   => self::merchantCommonRules('compareEndAt'),
        ];
        $messages = [];

        return self::validateCheck($request->all(), $rules, $messages);
    }

    public static function indexCharts(Request $request)
    {
        $rules    = [
            'showTimeTab'    => self::merchantCommonRules('showTimeTab'),
            'showStartAt'    => self::merchantCommonRules('showStartAt'),
            'showEndAt'      => self::merchantCommonRules('showEndAt'),
            'compareTimeTab' => self::merchantCommonRules('compareTimeTab'),
            'compareStartAt' => self::merchantCommonRules('compareStartAt'),
            'compareEndAt'   => self::merchantCommonRules('compareEndAt'),
            'statisticsType' => self::merchantCommonRules('statisticsType'),
        ];
        $messages = [];

        return self::validateCheck($request->all(), $rules, $messages);
    }

    public static function combineOrderOverview(Request $request)
    {
        $rules    = [
            'showTimeTab' => self::merchantCommonRules('showTimeTab'),
            'showStartAt' => self::merchantCommonRules('showStartAt'),
            'showEndAt'   => self::merchantCommonRules('showEndAt'),
            'orderType'   => self::merchantCommonRules('orderType'),
        ];
        $messages = [];

        return self::validateCheck($request->all(), $rules, $messages);
    }

    public static function combineOrderChart(Request $request)
    {
        $rules    = [
            'showTimeTab'    => self::merchantCommonRules('showTimeTab'),
            'showStartAt'    => self::merchantCommonRules('showStartAt'),
            'showEndAt'      => self::merchantCommonRules('showEndAt'),
            'orderType'      => self::merchantCommonRules('orderType'),
            'statisticsType' => self::merchantCommonRules('statisticsType'),
        ];
        $messages = [];

        return self::validateCheck($request->all(), $rules, $messages);
    }

    public static function combineOrderList(Request $request)
    {
        $rules    = [
            'showTimeTab' => self::merchantCommonRules('showTimeTab'),
            'showStartAt' => self::merchantCommonRules('showStartAt'),
            'showEndAt'   => self::merchantCommonRules('showEndAt'),
            'orderType'   => self::merchantCommonRules('orderType'),
            'pageSize'    => self::commonRules('pageSize'),
            'pageNum'     => self::commonRules('pageNum'),
        ];
        $messages = [];

        return self::validateCheck($request->all(), $rules, $messages);
    }

    private static function merchantCommonRules(string $param)
    {
        return [
                'showTimeTab'    => [
                    'required',
                    'integer',
                    function ($attribute, $value, $fail) {
                        if (!in_array($value, array_keys(OrderMerchantEnum::SHOW_TIME_TAB_TEXT))) {
                            $fail("不支持的showTimeTab");
                        }
                    }
                ],
                'showStartAt'    => 'nullable|integer|min:0',
                'showEndAt'      => 'nullable|integer|min:0',
                'orderType'      => [
                    'required',
                    'integer',
                    function ($attribute, $value, $fail) use ($param) {
                        if (!in_array($value, array_keys(OrderMerchantEnum::ORDER_TYPE_TEXT))) {
                            $fail("不支持的orderType");
                        }
                    }
                ],
                'statisticsType' => [
                    'required',
                    'integer',
                    function ($attribute, $value, $fail) use ($param) {
                        if (!in_array($value, array_keys(OrderMerchantEnum::STAT_TEXT))) {
                            $fail("不支持的statisticsType");
                        }
                    }
                ],
                'compareTimeTab' => [
                    'required',
                    'integer',
                    function ($attribute, $value, $fail) use ($param) {
                        if (!in_array($value, array_keys(OrderMerchantEnum::COMPARE_TIME_TAB_TEXT))) {
                            $fail("不支持的compareTimeTab");
                        }
                    }
                ],
                'compareStartAt' => 'nullable|integer|min:0',
                'compareEndAt'   => 'nullable|integer|min:0',
            ][$param] ?? '';
    }
}
